package UI;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import data_management.Main;
import data_management.PermissionManager;
import data_management.User;


public class SignInInterface extends JPanel implements ActionListener {


	public static final SignInInterface signIn = new SignInInterface();
	private static Image background = null;

	public JTextField userField;
	public JPasswordField passField;

	public String username;
	public String password;

	//@ public invariant username != null;
	//@ public invariant username != null;


	public void initialize() {

		try {
			background = ImageIO.read(ClassLoader.getSystemResourceAsStream("blue-sky-background.jpg"));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		BorderLayout layout = new BorderLayout();

		setLayout(layout);

		JPanel north = null;
		try {
			north = new JPanel() {
				private Image image = ImageIO.read(ClassLoader.getSystemResourceAsStream("newbirdlogo.png")).getScaledInstance(111, 200, Image.SCALE_FAST);
				private Image text = ImageIO.read(ClassLoader.getSystemResourceAsStream("newbird.png")).getScaledInstance(320, 200, Image.SCALE_FAST);
				protected void paintComponent(java.awt.Graphics g) {
					super.paintComponent(g);
					g.drawImage(text, getWidth() / 2 - text.getWidth(null) / 2, getHeight() / 2 - 200, null);

					g.drawImage(image, getWidth() / 2 - image.getWidth(null) / 2,
							getHeight() / 2 - image.getHeight(null) / 2, null);
				};
			};
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/* START FILLER PANELS */
		north.setPreferredSize(new Dimension(0,350));
		north.setOpaque(false);
		add(north, BorderLayout.NORTH);
		JPanel west = new JPanel();
		west.setOpaque(false);
		west.setPreferredSize(new Dimension(200,0));
		add(west, BorderLayout.WEST);
		JPanel east = new JPanel();
		east.setOpaque(false);
		east.setPreferredSize(new Dimension(200,0));
		add(east, BorderLayout.EAST);
		JPanel south = new JPanel();
		south.setOpaque(false);
		south.setPreferredSize(new Dimension(0,75));
		add(south, BorderLayout.SOUTH);
		/* END FILLER PANELS */

		// add our logo

		// A container to hold two panels, the labels and fields
		JPanel centerPanel = new JPanel();
		centerPanel.setOpaque(false);
		add(centerPanel, BorderLayout.CENTER);

		// Our panel for the labels, like Username: and Password:
		JPanel labelPanel = new JPanel();
		JLabel userLabel = new JLabel("Username:");
		userLabel.setOpaque(false);
		userLabel.setAlignmentX(RIGHT_ALIGNMENT);
		labelPanel.add(userLabel);

		JLabel passLabel = new JLabel("Password:");
		passLabel.setOpaque(false);
		passLabel.setAlignmentX(RIGHT_ALIGNMENT);
		labelPanel.add(passLabel);

		labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.PAGE_AXIS));

		centerPanel.add(labelPanel);

		// Our panel for the text fields, should be contained inside centerPanel
		JPanel fieldPanel = new JPanel();
		fieldPanel.setOpaque(false);
		fieldPanel.setLayout(new BoxLayout(fieldPanel, BoxLayout.PAGE_AXIS));
		centerPanel.add(fieldPanel);

		userField = new JTextField(10);
		userField.setMaximumSize(new Dimension(150, 20));
		userField.addActionListener(this);
		fieldPanel.add(userField);

		passField = new JPasswordField();
		passField.setMaximumSize(new Dimension(150, 20));
		passField.addActionListener(this);
		fieldPanel.add(passField);

		setPreferredSize(new Dimension(Main.DEFAULT_WIDTH, Main.DEFAULT_HEIGHT));
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(background, 0, 0, null);
	}

	/*@
	 @ requires PermissionManager.getInstance() != null;
	 @*/
	public void attemptLogin() {

		User user = PermissionManager.getInstance().loginUser(username, password);
		if (user != null) {
			// transfer user into logged in gui/interfaces
			performLogin();
		}
	}

	public void performLogin() {
		// switch to the tabbed pane with all our interfaces

		Main.getInstance().remove(this);

		JTabbedPane tabPane = new JTabbedPane();
		Main.getInstance().add(tabPane);

		tabPane.addTab("Inventory", InventoryInterface.getInstance());
		tabPane.addTab("Sale", SaleInterface.getInstance());
		Main.getInstance().repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		Object source = e.getSource();

		if (source == userField) {
			// the user pressed enter, let's advance the cursor to the password field
			Robot robot = null;
			try {
				robot = new Robot();
			} catch (AWTException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			robot.keyPress(KeyEvent.VK_TAB);
			robot.keyRelease(KeyEvent.VK_TAB);
		}
		else {
			// attempt login
			username = userField.getText();
			password = new String(passField.getPassword());

			attemptLogin();
		}
	}

	public static SignInInterface getInstance() {

		return signIn;
	}

}
